package com.aquan.map_jetpack

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.view.WindowCompat
import com.aquan.map_jetpack.ui.theme.Map_jetpackTheme
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import androidx.compose.ui.graphics.Color
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.aquan.map_jetpack.ui.components.NavHostApp
import com.aquan.map_jetpack.ui.screens.MainFrame
import com.google.accompanist.navigation.animation.rememberAnimatedNavController


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        WindowCompat.setDecorFitsSystemWindows(window, false)
        // 授权
        initPermission()


        setContent {
            Map_jetpackTheme {
                // A surface container using the 'background' color from the theme
                //  TODO 系统样式修改
//                val systemUiController = rememberSystemUiController()
                SideEffect {
//                    TODO 设置为透明
//                    systemUiController.setStatusBarColor(Color.Transparent, darkIcons = false)
                }

                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    NavHostApp()
                }
            }
        }
    }

    //1、首先声明一个数组permissions，将需要的权限都放在里面
    private val permissions = arrayOf(
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_PHONE_STATE,
        Manifest.permission.INTERNET,
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION,
    )
    private val mPermissionList = ArrayList<String>()
    private val mRequestCode = 0x1//权限请求码
    private fun initPermission() {
        mPermissionList.clear()
        for (permission in permissions) {
            if (ContextCompat.checkSelfPermission(
                    this@MainActivity,
                    permission
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                mPermissionList.add(permission)
            }
        }
        if (mPermissionList.isNotEmpty()) {
            // 后续操作...
            ActivityCompat.requestPermissions(this@MainActivity, permissions, mRequestCode)

        } else {
            Toast.makeText(this@MainActivity,"全部授予！",Toast.LENGTH_SHORT).show()
        }
    }
    //重写
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            0x1 -> for (element in grantResults) {
                if (element != PackageManager.PERMISSION_GRANTED) Toast.makeText(
                    this,
                    "您有未授予的权限，可能影响使用",
                    Toast.LENGTH_SHORT
                ).show()
            }

        }// 授权结束后的后续操作
    }
}

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    Map_jetpackTheme {
        Greeting("Android")
    }
}